<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Update_Trouble extends Page
{
       public $Label2 = null;
       public $btnShowType = null;
       public $JTJavaScript1 = null;
       public $btnUpdate = null;
       public $hiddenId = null;
       public $cmbServicedBy = null;
       public $btnCancel = null;
       public $dpStart = null;
       public $dpEnd = null;
       public $cmbRemedy = null;
       public $cmbTrouble = null;
       public $Label1 = null;
       public $Label86 = null;
       public $Label85 = null;
       public $Label93 = null;
       public $Image1 = null;
       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       var win_height = 470;
       var win_width = 756;
       var height = window.opener.outerHeight;
       var width = window.opener.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("mini_trouble_remedy_droplist.php", 'Edit_Access_Level', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function Update_TroubleJSLoad($sender, $params)
       {

       ?>
       var trouble = document.getElementById('cmbTrouble').value;
       troubleRemedy();
       <?php

       }

       function btnCancelJSClick($sender, $params)
       {

       ?>
       window.close();
       <?php

       }

       function cmbTroubleJSChange($sender, $params)
       {

       ?>
       var trouble = document.getElementById('cmbTrouble').value;
       troubleRemedy();
       return false;
       <?php

       }

       function Update_TroubleCreate($sender, $params)
       {
       $trouble = $_GET['trouble'];

       if($trouble!='' || $trouble!=null)
       $this->hiddenId->setValue($trouble);

       $query_trouble = "SELECT * FROM veh_trouble_tbl where autoid = '".$this->hiddenId->getValue()."'";
       $result_trouble = mysql_query($query_trouble)or die(mysql_error());
       $row_trouble = mysql_fetch_array($result_trouble);

       $this->dpStart->Text = $row_trouble['start_date'];
       $this->dpEnd->Text = $row_trouble['end_date'];

       /*$this->cmbServicedBy->Clear();
       $this->cmbServicedBy->ItemIndex = $row_trouble['serviced_by'];
       $result_ServiceProvider = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='serviced by'");

       while($row_ServiceProvider = mysql_fetch_array($result_ServiceProvider))
        {
            $this->cmbServicedBy->AddItem($row_ServiceProvider['value'], null, $row_ServiceProvider['value']);
        }*/

       $this->cmbServicedBy->Clear();
       $this->cmbServicedBy->ItemIndex = $row_trouble['serviced_by'];
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbServicedBy->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }


       $this->cmbTrouble->Clear();
       $result_Trouble = mysql_query("select * from trouble_droplist_tbl where company_id = '".$_SESSION['company_id']."'");
       while($row_Trouble = mysql_fetch_array($result_Trouble))
        {
            $this->cmbTrouble->AddItem($row_Trouble['value'], null, $row_Trouble['autoid']);
        }
       $this->cmbTrouble->setItemIndex($row_trouble['trouble']);


       $query_remedy = "SELECT * FROM remedy_droplist_tbl WHERE 	trouble_droplist_id='".$row_trouble['trouble']."'";
       //die($query_remedy);
       $result_remedy = mysql_query($query_remedy) or die("SELECT REMEDY ".mysql_error());
       $this->cmbRemedy->Clear();
       while($row_remedy = mysql_fetch_array($result_remedy))
       {
        $this->cmbRemedy->AddItem($row_remedy['value'],'',$row_remedy['value']);
       }
       $this->cmbRemedy->setItemIndex($row_trouble['remedy']);
       }

       function btnUpdateClick($sender, $params)
       {
       mysql_query("update veh_trouble_tbl set
       start_date = '" . $this->dpStart->Text . "',
       end_date = '" . $this->dpEnd->Text . "',
       trouble = '" . $this->cmbTrouble->getItemIndex() . "',
       remedy = '" . $this->cmbRemedy->getItemIndex() . "',
       serviced_by = '" . $this->cmbServicedBy->getItemIndex() . "'
       where autoid='" . $this->hiddenId->getValue() . "'")or die("UPDATE TROUBLE - " . mysql_error());

       echo "<script type=text/javascript>
        alert('Successfully updated record!');
        window.opener.location.reload();
        window.close();
        </script>";

       }

}

global $application;

global $Update_Trouble;

//Creates the form
$Update_Trouble=new Update_Trouble($application);

//Read from resource file
$Update_Trouble->loadResource(__FILE__);

//Shows the form
$Update_Trouble->show();

?>